package org.anyin.gitee.cloud.center.upms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anyin.gitee.cloud.center.upms.controller.form.SysMenuPageQueryForm;
import org.anyin.gitee.cloud.center.upms.controller.form.SysMenuSaveForm;
import org.anyin.gitee.cloud.center.upms.convert.SysMenuConvert;
import org.anyin.gitee.cloud.center.upms.service.SysMenuService;
import org.anyin.gitee.cloud.center.upms.service.dto.SysMenuInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysMenuNodeDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysMenuPageQueryDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysMenuSaveDTO;
import org.anyin.gitee.cloud.lib.core.web.ApiResponse;
import org.anyin.gitee.cloud.lib.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys-menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysMenuConvert sysMenuConvert;

    @PostMapping("/page")
    public ApiResponse page(@RequestBody SysMenuPageQueryForm form){
        SysMenuPageQueryDTO param = sysMenuConvert.getSysMenuPageQueryDTO(form);
        Page<SysMenuInfoDTO> page = sysMenuService.page(param);
        return ApiResponse.success(page);
    }

    @PostMapping("/create")
    public ApiResponse create(@RequestBody SysMenuSaveForm form){
        SysMenuSaveDTO param = sysMenuConvert.getSysMenuSaveDTO(form);
        sysMenuService.create(param);
        return ApiResponse.success();
    }

    @PostMapping("/modify/{id}")
    public ApiResponse modify(@PathVariable("id") Long id, @RequestBody SysMenuSaveForm form){
        SysMenuSaveDTO param = sysMenuConvert.getSysMenuSaveDTO(form);
        sysMenuService.modify(id, param);
        return ApiResponse.success();
    }

    @GetMapping("/info/{id}")
    public ApiResponse info(@PathVariable("id") Long id){
        SysMenuInfoDTO info = sysMenuService.info(id);
        return ApiResponse.success(info);
    }

    @PostMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id){
        sysMenuService.delete(id);
        return ApiResponse.success();
    }

    @GetMapping("/tree")
    public ApiResponse tree(){
        List<SysMenuNodeDTO> tree = sysMenuService.tree();
        return ApiResponse.success(tree);
    }
}
